package com.citywy.controller;

import com.citywy.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/anno")
public class AnnoController {

    @RequestMapping("/userRequestParam")
    public String anno1(@RequestParam(name = "username") String name,@RequestParam(value="age",required = false) Integer age){
        System.out.println(name+"--"+age);
        return "pages/success";
    }

    @RequestMapping("userRequestBody")
    public String anno2(@RequestBody(required = false) String body){
        System.out.println("进来requestBody");
        System.out.println(body);
        return "pages/success";
    }

    @RequestMapping("userPathVariable/{id:\\d+}/{onecode:[a-z]+}/{twocode:[a-z]+}")
    public String anno3(@PathVariable(name = "id") Integer id, @PathVariable(value = "onecode",required = false) String code1, @PathVariable("twocode") String code2, @RequestHeader(value = "Accept-Language",required = false) String header){
        System.out.println(id+"--"+code1+"--"+code2);
        System.out.println("请求头："+header);
        return "pages/success";
    }

    @RequestMapping("userCookieValue")
    public String anno4(@CookieValue(name = "JSESSIONID",required = false) String cookie){
        System.out.println(cookie);
        return "pages/success";
    }

    //基于pojo属性的基本使用
    /*@ModelAttribute
    public void showModel(User user){
        System.out.println("1:执行showModel方法:"+user.getName());
        user.setName("aote");
    }*/

    @ModelAttribute
    public void showModel(String name, Map<String,User> map){
        //模拟数据库查询
        User u = findUserByName(name);
        System.out.println("2:执行showModel方法"+u);
        map.put("aaa",u);
    }

    /**
     * 模拟查询数数据库
     * @return
     */
    private User findUserByName(String name){
        User u = new User();
        u.setId(12);
        u.setName("rayta");
        u.setAddress("北京");
        u.setAge(22);
        return u;
    }


    @RequestMapping("testModelAttribute")
    public String anno5(@ModelAttribute("aaa") User user){
        System.out.println("执行控制器方法："+user);
        return "pages/success";
    }
//    2:执行showModel方法User{id=12, name='rayta', address='北京', age=22, birthday=null}
//    执行控制器方法：User{id=12, name='traray', address='北京', age=19, birthday=null}



}

